package com.hrma.config;

import com.hrma.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * create by 2023/1/13.
 *
 * @author udbjqr
 */
@Slf4j
public class MyInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String token = request.getHeader("Authorization");
		int userId = UserUtil.getUserIdByToken(token);

		//保存当前用户
		UserUtil.saveUser(userId);
		//在返回的头里面设置新的token
		response.setHeader("Authorization", UserUtil.getToken(userId));
		return true;
	}
}
